Crate atomic_traits
source ·Expand description
The traits for generic atomic operations
§Compatibility
The crate is tested for rustc 1.34 and greater.
§Example
use std::sync::atomic::{AtomicUsize, Ordering};
use num_traits::One;
use atomic_traits::{Atomic, NumOps, fetch};
#[derive(Debug, Default)]
pub struct RefCnt<T>(T);
impl<T> RefCnt<T>
where
T: Atomic + NumOps + Default,
<T as Atomic>::Type: One
{
pub fn inc(&self) -> <T as Atomic>::Type {
self.0.fetch_add(<T as Atomic>::Type::one(), Ordering::Acquire)
}
pub fn dec(&self) -> <T as Atomic>::Type {
self.0.fetch_sub(<T as Atomic>::Type::one(), Ordering::Release)
}
pub fn val(&self) -> <T as Atomic>::Type {
self.0.load(Ordering::SeqCst)
}
}
let refcnt = RefCnt::<AtomicUsize>::default();
assert_eq!(refcnt.inc(), 0);
assert_eq!(refcnt.dec(), 1);
assert_eq!(refcnt.val(), 0);
Modules§
- Fetch and apply operations to the current value, returning the previous value.
Traits§
- Returns a mutable pointer to the underlying type.
- Generic atomic types
- The trait for types implementing atomic bitwise operations
- Creates a new atomic type from a pointer.
- The trait for types implementing atomic numeric operations